package com.enqualcomm.kids.view.heartrate;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.enqualcomm.kids.R;

import java.util.ArrayList;
import java.util.List;

import common.utils.MyLogger;

/**
 * Created by Sunzhiang on 2016/7/18.
 */
public class HeartRateSetBarView extends View {

    private static final int THUMB_SIZE = 50;

    private Paint paint = new Paint();
    private Paint selectedPaint = new Paint();
    private int mHeartRate_nums = 2;
    private float mLineHeight;
    private float mThumbRadius;
    private float mCircleRadius;
    private float mPadding;
    private int mProgressColor = Color.YELLOW;
    private int mBarColor = Color.parseColor("#c4c6bf");

    private float mCenterY;
    private float mLeftX;
    private float mLeftY;
    private float mRightX;
    private float mRightY;
    private float mDelta;
    private List<Float> mThumbContainerXPosition = new ArrayList<>();
    private float mTwoPointdistenceLength;


    public HeartRateSetBarView(Context context) {
        super(context);
    }

    public HeartRateSetBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.HeartRateSetBarView);
        mHeartRate_nums = a.getInt(R.styleable.HeartRateSetBarView_heart_rate_nums, 0);
        a.recycle();

        init();
    }

    public HeartRateSetBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.HeartRateSetBarView);
        mHeartRate_nums = a.getInt(R.styleable.HeartRateSetBarView_heart_rate_nums, 0);
        a.recycle();

        init();
    }

    private void init() {
        mLineHeight = 0.2f * THUMB_SIZE;
        mThumbRadius = 0.4f * THUMB_SIZE;
        mCircleRadius = 0.4f * mThumbRadius;
        mPadding = 0.5f * THUMB_SIZE;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterY = 0.5f * getHeight();
        mLeftX = mPadding;
        mLeftY = mCenterY - (mLineHeight / 2);
        mRightX = getWidth() - mPadding;
        mRightY = 0.5f * (getHeight() + mLineHeight);
        mDelta = (mRightX - mLeftX) / (mHeartRate_nums - 1);

        mThumbContainerXPosition.add(mLeftX);
        for (int i = 1; i < mHeartRate_nums - 1; i++) {
            mThumbContainerXPosition.add(mLeftX + (i * mDelta));
        }
        mThumbContainerXPosition.add(mRightX);
        mTwoPointdistenceLength = mThumbContainerXPosition.get(1) - mThumbContainerXPosition.get(0);
//        mDrawListener.onReady();
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Draw rect bounds
        paint.setAntiAlias(true);
        paint.setColor(mBarColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);

        selectedPaint.setAntiAlias(true);
        selectedPaint.setColor(Color.parseColor("#f3f1e9"));
        selectedPaint.setStyle(Paint.Style.STROKE);
        selectedPaint.setStrokeWidth(2);

        // Draw rest of the circle'Bounds
        for (int i = 0; i < mThumbContainerXPosition.size(); i++) {
            canvas.drawCircle(mThumbContainerXPosition.get(i), mCenterY, mCircleRadius, paint);
        }

        paint.setStyle(Paint.Style.FILL);
        selectedPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < mThumbContainerXPosition.size() - 1; i++) {
            final float pos = mThumbContainerXPosition.get(i);
            final float pos2 = mThumbContainerXPosition.get(i + 1);
            canvas.drawRect(pos, mLeftY, pos2, mRightY, paint);
        }

        // Draw rest of circle
        for (int i = 0; i < mThumbContainerXPosition.size(); i++) {
            final float pos = mThumbContainerXPosition.get(i);
            canvas.drawCircle(pos, mCenterY, mCircleRadius, paint);
        }
        /*画thumber*/
//        if (isTouchUp) { //滑动结束需要确定位置
//            canvas.drawCircle(mTouchUpLeftX, mCenterY, mThumbRadius, selectedPaint);
//        } else {
//            canvas.drawCircle(mTouchLeftX, mCenterY, mThumbRadius, selectedPaint);
//        }
        listener.onReady(mThumbContainerXPosition.get(0), mThumbContainerXPosition.get(mHeartRate_nums - 1), mTwoPointdistenceLength,mHeartRate_nums);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 200;
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        int height = THUMB_SIZE + 20;
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(width, height);
    }

    public static int getColorWithAlpha(int color, float ratio) {
        int newColor = 0;
        int alpha = Math.round(Color.alpha(color) * ratio);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        newColor = Color.argb(alpha, r, g, b);
        return newColor;
    }



    //    private void animateMoveThumeToPosition(int leftX){
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "number", 0, progressCurrent);
//        objectAnimator.setDuration(500);
//        //加速器，从慢到快到再到慢
//        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
//        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                currenttextAngle = (float) animation.getAnimatedValue();
//                curValues = currenttextAngle / k;
//            }
//        });
//        objectAnimator.start();
//    }
    private onDrawReadyListener listener;

    public void setOnDrawReadyListener(onDrawReadyListener listener) {
        this.listener = listener;
    }

    public interface onDrawReadyListener {
        void onReady(float startX, float endX, float singleLength,int nums);
    }


}
